package com.situ.lession1226.service.impl;

import com.situ.lession1226.dao.RecommendDAO;
import com.situ.lession1226.model.Rating;
import com.situ.lession1226.service.RatingService;
import com.situ.lession1226.service.RecommendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
@Service
public class RecommendServiceImpl implements RecommendService {
    private RecommendDAO dao;

    @Autowired
    public void setDao(RecommendDAO dao) {
        this.dao = dao;
    }



    @Override
    public List<Rating> findRecommend(String userid) {

//        List<Rating> Ratings = dao.findRecommend(userid);
        List<Rating> Ratings =new ArrayList<>();
//        判断评分数量
        String[] type={"剧情","爱情","科幻","喜剧","动作"};
        String types=type[0];
        int a1=dao.findSum(userid,types);
        types=type[1];
        int a2=dao.findSum(userid,types);
        types=type[2];
        int a3=dao.findSum(userid,types);
        types=type[3];
        int a4=dao.findSum(userid,types);
        types=type[4];
        int a5=dao.findSum(userid,types);

        int sum=a1+a2+a3+a4+a5;

        if(sum<=10){
            Ratings.addAll(dao.findTop(type[0],2));
            Ratings.addAll(dao.findTop(type[1],2));
            Ratings.addAll(dao.findTop(type[2],2));
            Ratings.addAll(dao.findTop(type[3],2));
            Ratings.addAll(dao.findTop(type[4],2));
        }else{
            a1=10*a1/sum;
            a2=10*a2/sum;
            a3=10*a3/sum;
            a4=10*a4/sum;
            a5=10*a5/sum;

            if(a1>0){
                Ratings.addAll(dao.findTop(type[0],a1));
            }if(a2>0){
                Ratings.addAll(dao.findTop(type[1],a2));
            }if(a3>0){
                Ratings.addAll(dao.findTop(type[2],a3));
            }if(a4>0){
                Ratings.addAll(dao.findTop(type[3],a4));
            }if(a5>0){
                Ratings.addAll(dao.findTop(type[4],a5));
            }

        }
        System.out.println(a1+a2+a3+a4+a5);
        return Ratings;

    }

}